package com.itheima.travel.web;

import com.itheima.travel.enums.StatusEnum;
import com.itheima.travel.exception.ProjectException;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.res.ResponseWrap;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.utils.ExceptionsUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.ws.soap.Addressing;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/affix")
@Log4j2
@CrossOrigin
public class AffixController {

    @Autowired
    private AffixService affixService;

    @PostMapping("upload/{businessType}")
    public ResponseWrap<AffixVo> upload(MultipartFile multipartFile,@PathVariable("businessType") String businessType) throws IOException, ProjectException {
        // 封装请求信息
        AffixVo affixVo = AffixVo.builder().businessType(businessType).build();
        AffixVo affixVoResult = null;
        try
        {
           affixVoResult = affixService.upLoad(multipartFile,affixVo);
           return ResponseWrap.<AffixVo>builder()
                   .code(StatusEnum.SUCCEED.getCode())
                   .msg(StatusEnum.SUCCEED.getMsg())
                   .operationTime(new Date())
                   .data(affixVoResult)
                   .build();
        }catch (IOException e){
            log.error("文件上传异常:{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.UPLOAD_FAIL.getCode(),StatusEnum.UPLOAD_FAIL.getMsg());
        }
    }

    @PostMapping("bindBusinessId")
    // 图片绑定业务
    public ResponseWrap<Boolean> bindBusinessId(@RequestBody AffixVo affixVo) throws ProjectException {
        try{
            Boolean flag = affixService.bindBusinessId(affixVo);
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e)
        {
            log.error("附件绑定业务异常:{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.BIND_BUSINESS_FAIL.getCode(),StatusEnum.BIND_BUSINESS_FAIL.getMsg());

        }
    }

    @GetMapping("{businessId}")
    public ResponseWrap<List<AffixVo>> findAffixByBusinessId(@PathVariable("businessId") Long business) throws ProjectException {
        try{
            AffixVo affixVo = AffixVo.builder().businessId(business).build();
            List<AffixVo> affixVoList = affixService.findAffixByBusinessId(affixVo);
            return ResponseWrap.<List<AffixVo>>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(affixVoList)
                    .build();
        }catch (Exception e){
            log.error("查询业务对应附件异常:{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FIND_AFFIX_BUSINESSID_FAIL.getCode(),StatusEnum.FIND_AFFIX_BUSINESSID_FAIL.getMsg());
        }
    }
}
